﻿using WpfApp1.Models;

namespace WpfApp1.ViewModels
{
    public class LoginViewModel : BindableBase, IDialogAware
    {
        private LoginModel _loginModel;

        private DelegateCommand _loginCommand;
        public DelegateCommand LoginCommand => _loginCommand ??= new DelegateCommand(OnLogin);

        public LoginViewModel()
        {
            _loginModel = new LoginModel();
        }

        private void OnLogin()
        {
            RequestClose.Invoke(new DialogResult(ButtonResult.OK));
        }

        private bool CanLoginExecute(object parameter)
        {
            if (string.IsNullOrEmpty(UserName) || string.IsNullOrEmpty(Password))
            {
                return false;
            }
            return true;
        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }

        public void OnDialogOpened(IDialogParameters parameters)
        {

        }

        public string UserName
        {
            get
            {
                return _loginModel.UserName;
            }
            set
            {
                _loginModel.UserName = value;
                RaisePropertyChanged(nameof(UserName));
            }
        }

        public string Password
        {
            get
            {
                return _loginModel.Password;
            }
            set
            {
                _loginModel.Password = value;
                RaisePropertyChanged(nameof(Password));
            }
        }

        private string _title = "登录";
        public string Title
        {
            get { return _title; }
            set { SetProperty(ref _title, value); }
        }

        public DialogCloseListener RequestClose { get; }
    }
}
